$(document).ready(function() {
	$(".map_selected_detail_frame").hide();
	$(".hospital_detail").hide();
	$("div[class^='map_']").click($.fn.mapGetRegionDetailList);
});

$.fn.mapGetRegionDetailList = function (){
	$(".hospital_detail").hide();
	var mapLocation = $(this).attr('val');
	$.ajax({
		 type: "POST",
		 url: 'SearchByServlet',
		 data: {
			 ACTION : "EXPERTISEBYREGION",
			 mapLocation : mapLocation
			},
		 dataType: "json",
		 success: function(data){
			var tableLayout = genExpertiseByRegion(data);
			$(".map_selected_detail_frame").show();
			$(".tbl_selected_detail_content").html(tableLayout);
		 },
		error : function(xhr, ajaxOptions, thrownError) {
			alert("Error while processing.");
		}
	});
}

function genExpertiseByRegion(data){
	var loops = data.length;
	var tableLayout = "";
	if(loops > 0){
		tableLayout += "<tr><th>"+data[0].region+"</th></tr><tr><th>Name</th></tr>";
		for(var i=0; i<loops; i++){
			tableLayout += "<tr><td onclick='gotoExpertiseItem("+data[i].expertiseId+")'>"+
			data[i].expertiseName+"</td></tr>";
		}
	}
	return tableLayout;
}

function gotoExpertiseItem(expertiseId){
	$.ajax({
		 type: "POST",
		 url: 'SearchByServlet',
		 data: {
			 ACTION: "EXPERTISEITEM",
			 expertiseId : expertiseId
			},
		 dataType: "json",
		 success: function(data){
			var tableLayout = genExpertiseItem(data);
			$(".hospital_detail").show();
			$(".tbl_hospital_detail").html(tableLayout);
		 },
		error : function(xhr, ajaxOptions, thrownError) {
			alert("Error while processing.");
		}
	});
}

function genExpertiseItem(data){
	var loops = data.length;
	var tableLayout = "";
	if(loops > 0){
		tableLayout += "<tr><th style='cursor:pointer;' colspan=2 onclick='gotoProductByExpertise("+data[0].expertiseId+")'>"+data[0].region+"<br>"+data[0].expertiseName+"</th></tr>";
		for(var i=0; i<loops; i++){
			var eaLoops = data[i].expertiseAgentList.length;
			for(var j=0; j<eaLoops; j++){
				tableLayout += "<tr><td>Location : "+data[i].expertiseAgentList[j].location+"</td></tr>";
				tableLayout += "<tr><td>Name : "+data[i].expertiseAgentList[j].name+"</td></tr>";
				tableLayout += "<tr><td>Position : "+data[i].expertiseAgentList[j].position+"</td></tr>";
				tableLayout += "<tr><td>Tel : "+data[i].expertiseAgentList[j].telephone+"</td></tr>";
				tableLayout += "<tr><td>E-mail : "+data[i].expertiseAgentList[j].email+"</td></tr>";
				tableLayout += "<tr><td><hr noshade></td></tr>";
			}
		}
	}
	return tableLayout;
}

function gotoProductByExpertise(expertiseId){
	window.location = 'ProductServlet?expertiseId='+expertiseId;
}